package com.manage.system.svc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manage.common.core.constants.Constant;
import com.manage.common.core.result.JsonResult;
import com.manage.common.core.utils.PageUtil;
import com.manage.common.core.utils.StringUtils;
import com.manage.common.redis.generator.RedisIdGenerator;
import com.manage.system.api.entity.SysLoginLog;
import com.manage.system.api.request.log.LoginLogQueryParam;
import com.manage.system.api.service.ISysLoginLogService;
import com.manage.system.svc.mapper.SysLoginLogMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 登录日志 服务实现类
 * </p>
 *
 * @author zhangqing
 * @since 2021-07-14
 */
@Service
@AllArgsConstructor
@Slf4j
public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper, SysLoginLog> implements ISysLoginLogService {

    private final RedisIdGenerator redisIdGenerator;
    private final SysLoginLogMapper sysLoginLogMapper;

    @Override
    public void addLoginLog(SysLoginLog sysLoginLog) {
        sysLoginLog.setId(redisIdGenerator.nextId(Constant.SYS_LOGIN_LOG_KEY));
        this.baseMapper.insert(sysLoginLog);
    }

    @Override
    public JsonResult findLoginLogPage(LoginLogQueryParam loginLogQueryParam) {
        LambdaQueryWrapper<SysLoginLog> lambda = new QueryWrapper<SysLoginLog>().lambda()
                .eq(StringUtils.isNotNull(loginLogQueryParam.getStatus()),SysLoginLog::getStatus, loginLogQueryParam.getStatus())
                .eq(!StringUtils.isEmpty(loginLogQueryParam.getLoginName()),SysLoginLog::getLoginName, loginLogQueryParam.getLoginName())
                .eq(!StringUtils.isEmpty(loginLogQueryParam.getIpaddr()),SysLoginLog::getIpaddr, loginLogQueryParam.getIpaddr())
                .orderByDesc(SysLoginLog::getLoginTime);

        if(!StringUtils.isEmpty(loginLogQueryParam.getBeginTime())&&!StringUtils.isEmpty(loginLogQueryParam.getEndTime())){
            lambda.ge(SysLoginLog::getLoginTime,loginLogQueryParam.getBeginTime())
                    .le(SysLoginLog::getLoginTime,loginLogQueryParam.getEndTime());
        }
        PageUtil.startPageSort(loginLogQueryParam.getPageNum(), loginLogQueryParam.getPageSize(),
                loginLogQueryParam.getSortField(), loginLogQueryParam.getSortOrder());
        List<SysLoginLog> sysLoginLogs = sysLoginLogMapper.selectList(lambda);
        return JsonResult.success(PageUtil.restPage(sysLoginLogs));
    }

    @Override
    public JsonResult batchDelLoginLog(List<Long> ids) {
        log.info("系统服务->日志管理(登录日志)->批量删除登录日志->参数: ids:{}",ids.toString());
        return JsonResult.toJsonResult(sysLoginLogMapper.deleteBatchIds(ids));
    }

    @Override
    public JsonResult delAllLoginLog() {
        log.info("系统服务->日志管理(登录日志)->删除全部登录日志");
        return JsonResult.toJsonResult(sysLoginLogMapper.delete(Wrappers.emptyWrapper()));
    }

    @Override
    public JsonResult delOneLoginLog(Long loginLogId) {
        log.info("系统服务->日志管理(登录日志)->根据id,删除单个登录日志->参数: id:{}",loginLogId);
        return JsonResult.toJsonResult(sysLoginLogMapper.deleteById(loginLogId));
    }
}
